package com.lzk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzk.common.BaseUtils;
import com.lzk.common.R;
import com.lzk.mapper.ShoppingCartMapper;
import com.lzk.pojo.ShoppingCart;
import com.lzk.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart>implements ShoppingCartService {
    @Resource
    private ShoppingCartService shoppingCartService;
    @Override
    public R<List<ShoppingCart>> listAll() {
        Long userId= BaseUtils.getEmpId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(userId!=null,ShoppingCart::getUserId,userId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    @Override
    public R<String> add(ShoppingCart shoppingCart) {
        //获取用户id
        Long id = BaseUtils.getEmpId();
        //将id值赋给shoppingCart
        shoppingCart.setUserId(id);
        //设置时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //保存购物车
        shoppingCartService.save(shoppingCart);
        return R.success("添加购物车成功");
    }

    @Override
    public R<String> sub(ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        //判断传入的是套餐还是单个菜品
        if (shoppingCart.getSetmealId() == null) {//传入的是菜品的id
            wrapper.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {//传入的套餐的id
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //删除购物车里面指定的菜品或者套餐
        shoppingCartService.remove(wrapper);
        return R.success("删除成功");
    }

    @Override
    public R<String> clean() {
        //获取当前用户id
        Long id = BaseUtils.getEmpId();
        //根据用户id来获取购物车中的所有消息
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, id);
        //删除购物车中对应的信息
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
